﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DiagramDesigner.Common.Controls;
using DiagramDesigner.Common.Helpers;
using DiagramDesigner.Common.Interfaces;
using DiagramDesigner.Common.Services.Interfaces;
using DiagramDesigner.Module.Toolbox.Controls;
using Microsoft.Practices.ServiceLocation;
using Path = System.Windows.Shapes.Path;

namespace DiagramDesigner.Module.Toolbox.Views
{
    /// <summary>
    /// Interaction logic for ToolboxView.xaml
    /// </summary>
    public partial class ToolboxView : UserControl
    {

        public IEnumerable<IBehaviorPlugin> BehaviorPlugins { get; set; }
        public IEnumerable<IGeometryPlugin> ShapePlugins { get; set; }

        public ToolboxView()
        {
            InitializeComponent();
            
            ShapePlugins = ServiceLocator.Current.GetInstance<IPluginsLoaderService<IGeometryPlugin>>().GetPlugins();
            BehaviorPlugins = ServiceLocator.Current.GetInstance<IPluginsLoaderService<IBehaviorPlugin>>().GetPlugins();

            foreach (IGeometryPlugin plugin in ShapePlugins)
            {
                ToolboxItem toolboxItem = new ToolboxItem();
                DesignerShape shape = new DesignerShape();
                shape.Points = plugin.Points;
                shape.Geometry = plugin.GetGeometry();
                //Path path = new Path();
                //path.Stroke = Brushes.Black;
                //path.StrokeThickness = 1;
                //path.Fill = Brushes.Bisque;
                //path.Stretch = Stretch.Fill;
                //path.Data = plugin.GetGeometry();
                //path.SnapsToDevicePixels = true;
                DesignerItem designerItem = new DesignerItem();
                designerItem.Content = shape;
                toolboxItem.Content = designerItem;
                toolboxShapes.Items.Add(toolboxItem);
            }

            foreach (IBehaviorPlugin plugin in BehaviorPlugins)
            {
                if (!plugin.ShowInToobox) continue;
                ToolboxItem item = new ToolboxItem();
                Button button = new Button { Content = plugin.Icon};
                button.Click += BehaviourButtonClick;
                item.Content = button;
                button.Tag = plugin.ToolboxTitle;
                toolboxBehaviors.Items.Add(item);
            }
        }

        private void BehaviourButtonClick(object sender, RoutedEventArgs routedEventArgs)
        {
            Button button = sender as Button;
            foreach (IBehaviorPlugin plugin in BehaviorPlugins)
            {
                if (plugin.ToolboxTitle.Equals(button.Tag))
                {
                    foreach (DesignerItem item in CanvasHelper.Canvas.SelectedItems)
                    {
                        BehaviorCollection behaviors = Interaction.GetBehaviors(item);
                        var behavior = behaviors.SingleOrDefault(b => b.GetType() == plugin.BehaviorType);
                        if (behavior == null) behaviors.Add(plugin.GetBehaviour());
                    }
                    break;
                }
            }
        }
    }
}
